<?php
/**
 * 商品
 * Author: hades
 * DateTime: 2020/5/16 16:33
 */

namespace App\Controller\admin;

use App\Model\GCategory;
use App\Model\Goods;
use App\Validate\GoodsValidate;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;

/**
 * Class GoodsController
 * @package App\Controller\admin
 * @Controller(prefix="a/goods")
 * @Middleware(\App\Middleware\AdminMiddleware::class)
 */
class GoodsController extends AdminBase {

    /**
     * 列表
     * @RequestMapping(path="lists",methods="get")
     * @return array
     */
    public function getLists() {
        $size = $this->get('size');
        $size = pageSizeVerify($size);
        $condition = [];
        $result = Goods::query()
            ->where($condition)
            ->select('id', 'cid', 'name', 'price', 'stock_all', 'sales', 'cover')
            ->paginate($size);
        return success(paginate($result));
    }

    /**
     * 添加商品基本信息
     * @RequestMapping(path="info",methods="post")
     * @return array
     */
    public function postInfo() {
        $validate = $this->di->get(GoodsValidate::class);
        $validate->check($validate->add());
        $cid = $this->body('cid');
        $name = $this->body('name');
        $carousel = $this->imagesOssClear($this->body('carousel'));
        $bewrite = $this->imagesOssClear($this->body('bewrite'));
        if ($this->categoryCheck($cid) == 0) {
            return error('分类错误');
        }
        $res = Goods::query()->insert([
            'cid' => $cid,
            'name' => $name,
            'cover' => $carousel[0],
            'carousel' => implode(',', $carousel),
            'bewrite' => implode(',', $bewrite),
        ]);
        return $res ? success() : error();
    }

    /**
     * 多张图片,取出oss路径前缀
     * @param $path
     * @return array
     */
    private function imagesOssClear($path) {
        $path = explode(',', $path);
        foreach ($path as &$v) {
            $v = ossClear($path);
        }
        return $path;
    }

    /**
     * 判断分类ID是否正确
     * @param $cid
     * @return int
     */
    private function categoryCheck($cid) {
        return GCategory::query()
            ->where('id', $cid)
            ->where('parent_id', '>', 0)
            ->count();
    }

    /**
     * 修改商品基本信息
     * @RequestMapping(path="info",methods="put")
     * @return array
     */
    public function putInfo() {
        $validate = $this->di->get(GoodsValidate::class);
        $validate->check($validate->modify());
        $id = $this->body('id');
        $name = $this->body('name');
        $carousel = $this->imagesOssClear($this->body('carousel'));
        $bewrite = $this->imagesOssClear($this->body('bewrite'));
        Goods::query()->where('id', $id)
            ->update([
                'name' => $name,
                'cover' => $carousel[0],
                'carousel' => implode(',', $carousel),
                'bewrite' => implode(',', $bewrite),
            ]);
        return success();
    }

    /**
     * 上下架
     * @return array
     * @RequestMapping(path="sale",methods="post")
     */
    public function postSale() {
        $sale = $this->body('sale');
        $id = $this->body('id');
        $sale = $sale ? 1 : 0;
        $r = Goods::query()
            ->where('id', $id)
            ->update(['is_sale' => $sale]);
        return $r ? success() : error();
    }
}
